<template>
    <div class='smsLogin-wrapper'>
        <div class='login-tel'>
            <input type="text" v-model="userTel" placeholder="请输入手机号" pattern="[0-9]*">
        </div>
        <div class='login-code'>
            <input type="text" v-model="userCode" placeholder="请输入短信验证码" pattern="[0-9]*">
            <button :disabled="disabled" @click='sendCode'>{{ codeMsg }}</button>
        </div>
        <div class="login-btn">
            <button @click="login">登录</button>
        </div>
        <div class='tab'>
            <span @click='goPwdLogin'>密码登录</span>
            <span @click='goRegistration'>快速注册</span>
        </div>
    </div>
</template>

<script>
import { Toast } from 'mint-ui';
import { AddUserModel } from 'models/account';
import { CodeModel } from 'models/code'
import tools from 'utils/tools.js';
import { mapMutations } from 'vuex';

export default {
    name: 'SmsLoginWrapper',
    data() {
        return {
            disabled: false,
            userTel: '',
            userCode: '',
            codeNum: 6,
            codeMsg: '获取短信验证码',
            code: ''
        }
    },
    methods: {
        ...mapMutations(['userLogin']),
        //点击获取短信验证码按钮
        sendCode() {
            //验证
            if (!tools.validate('userTel',this.userTel)) return;

            //请求短信验证码接口
            const codeModel = new CodeModel();
            codeModel.code(this.userTel).then(res => {
                const data = res.data;

                console.log(data.msg +':'+data.data);
                if (data.success) {
                    this.code = data.data;
                }
            })
            //禁用按钮
            this.disabled = true;
            //倒计时
            let timer = setInterval(() => {
                --this.codeNum;
                this.codeMsg = `重新发送 ${this.codeNum}`;
            }, 1000)
            //判断什么时候停止定时器
            setTimeout(() => {
                clearInterval(timer);
                this.codeNum = 6;
                this.disabled = false;
                this.codeMsg = '获取短信验证码';
            }, 6000)
        },
        //密码登录
        goPwdLogin() {
            this.$router.push('/pwdLogin');
        },
        //快速注册
        goRegistration() {
            this.$router.push('/registration');
        },
        //点击登录
        login() {

            //前端验证
            if (!tools.validate('userTel', this.userTel)) return;
            if (!tools.validate('userPwd', this.userPwd)) return;
            
            //发送请求，后端验证
            if (this.code == this.userCode) {
                //证明用户输入的短信验证码是正确的
                //发送请求
                const addUserModel = new AddUserModel();
                addUserModel.addUser(this.userTel).then(res => {
                    const data = res.data;

                    //提示信息
                    Toast(data.msg);
                    //登录失败
                    if (!data.success) return;
                    //登录成功 ==》跳转页面，存储用户信息
                    this.userLogin(data.data);
                    //跳转到我的页面中
                    this.$router.push('/index');
                })

            }

        }
    }
}
</script>

<style scoped lang='scss'>
@import '~styles/mixins.scss';
@import '~styles/variables.scss';

.smsLogin-wrapper {
    @include flex-column;
    margin-top: 2.8rem;
    width: 100%;
    font-size: .16rem;
    box-sizing: border-box;

    .login-tel {
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
        box-sizing: border-box;

        input {
            width: 100%;
            height: 100%;
            border: 1px solid rgb(176, 223, 143);
            border-radius: .03rem;
            box-sizing: border-box;
            text-indent: .1rem;
        }
    }

    .login-code {
        display: flex;
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
        box-sizing: border-box;

        input {
            flex: 0 0 2rem;
            height: 100%;
            // width: 2rem;
            border: 1px solid rgb(176, 223, 143);
            border-radius: .03rem;
            box-sizing: border-box;
            text-indent: .1rem;
        }

        button {
            flex: 1;
            height: 100%;
            padding: 0 0.1rem;
            color: $defaultColor;
            background-color: rgb(176, 223, 143);
            border-radius: .03rem;
            box-sizing: border-box;
        }
    }

    .login-btn {
        height: .66rem;
        width: 100%;
        padding: .1rem .15rem;
        box-sizing: border-box;


        button {
            width: 100%;
            height: 100%;
            text-align: center;
            color: $defaultColor;
            background-color: rgb(176, 223, 143);
            border: 1px solid rgb(176, 223, 143);
            border-radius: .03rem;
            box-sizing: border-box;
        }
    }

    .tab {
        display: flex;
        justify-content: space-between;
        font-size: .14rem;
        padding: .05rem .15rem;
        box-sizing: border-box;
        color:rgb(176, 223, 143);
    }
}
</style>